
package com.android.finaltracer.usermanage;

import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.finaltracer.channel.ChannelProxy;
import com.android.finaltracer.engine.EngineService;
import com.android.finaltracer.engine.EventNotify;
import com.android.finaltracer.gps.Gps;
import com.android.finaltracer.model.CommandInfo;
import com.android.finaltracer.model.PositionInfo;
import com.android.finaltracer.model.Setting;
import com.android.finaltracer.model.SettingCreator;
import com.android.finaltracer.ui.Map;
import com.android.finaltracer.ui.ToShowMapActivity;

public class GpsTracerPhone extends Phone {
    private final static String TAG = GpsTracerPhone.class.getSimpleName();
    private Gps mGps = null;
    private Map mMap = null;

    // private int GpsUsedCount = 0;
    public static final int PHONE_STATUS_SEND_SIM_CHANGED = PHONE_STATUS_NULL + 1;// 2
    public static final int PHONE_STATUS_WIAT_HANDSHANKE_CNF = PHONE_STATUS_SEND_SIM_CHANGED + 1;// 3
    public static final int PHONE_STATUS_HANDSHANKE_CONNECTED = PHONE_STATUS_WIAT_HANDSHANKE_CNF + 1;// 4
    public static final int PHONE_STATUS_WAIT_GPS_INFO = PHONE_STATUS_HANDSHANKE_CONNECTED + 1;// 5
    public static final int PHONE_STATUS_SEND_GPS_INFO = PHONE_STATUS_WAIT_GPS_INFO + 1;// 6
    public static final int PHONE_STATUS_GPS_STOPED = PHONE_STATUS_SEND_GPS_INFO + 1;// 7

    public GpsTracerPhone(String phoneNum, Context mContext,
            EventNotify mEventNotify) {
        super(phoneNum, mContext, mEventNotify);
        mGps = Gps.getInstance(mContext, mEventNotify);
        mMap = Map.getInstance(mContext);
    }

    public boolean associatedPhoneCommandProcess(String from, int command) {
        Log.i(TAG, "associatedPhoneCommand:" + command);
        boolean remove = false;
        switch (command) {
            case CommandInfo.COMMAND_NOTIFY_SIM_CHANGED: {
                Log.i(TAG, "COMMAND_NOTIFY_SIM_CHANGED");
                showMap(from);
                break;
            }

            case CommandInfo.COMMAND_HANDSHAKE_REQ: {
                Log.i(TAG, "COMMAND_HAND_SHAKE_REQ");

                // Phone phone = mPm.addSlavePhone(from);
                setPhoneStatus(PHONE_STATUS_HANDSHANKE_CONNECTED);

                mChannelProxy.send(from, new CommandInfo(
                        CommandInfo.COMMAND_HANDSHAKE_CNF),
                        ChannelProxy.CHANNEL_SMS);
                FtTimer.startTimer(EngineService.MODULE_GPS_TRACER,
                        FtTimer.TIMER_ID_COMMAND_WAIT_MASTER_ORDER, from);
                retryCount++;
                break;
            }

            case CommandInfo.COMMAND_HANDSHAKE_CNF: {
                Log.i(TAG, "COMMAND_HANDSHAKE_CNF");

                // Phone phone = mPm.getSpecifyMasterPhone(from);
                Log.e(TAG, "number from is :" + from);
                Log.e(TAG, "phone is :" + this);

                if (getPhoneStatus() == PHONE_STATUS_WIAT_HANDSHANKE_CNF) {
                    setPhoneStatus(PHONE_STATUS_WAIT_GPS_INFO);
                    FtTimer.stopTimer(EngineService.MODULE_GPS_TRACER, from);
                    mChannelProxy.send(from, new CommandInfo(
                            CommandInfo.COMMAND_START_MONITOR_GPS_POSITION),
                            ChannelProxy.CHANNEL_SMS);
                    mChannelProxy
                            .startHttpRecvPositionReport(GPS_TRACER_HEART_BEAT);
                    FtTimer.startTimer(EngineService.MODULE_GPS_TRACER,
                            FtTimer.TIMER_ID_COMMAND_WAIT_GPS_INFO, from,
                            FtTimer.DEFAULT_LONG_DELAY);
                }
                break;
            }
            case CommandInfo.COMMAND_START_MONITOR_GPS_POSITION: {
                Log.i(TAG, "COMMAND_START_MONITOR_GPS_POSITION");
                // Phone phone = mPm.getSpecifySlavePhone(from);
                Log.e(TAG, "phone is :" + this);
                if (getPhoneStatus() == PHONE_STATUS_HANDSHANKE_CONNECTED) {
                    setPhoneStatus(PHONE_STATUS_SEND_GPS_INFO);
                    FtTimer.stopTimer(EngineService.MODULE_GPS_TRACER, from);
                    mGps.start();
                    FtTimer.startTimer(EngineService.MODULE_GPS_TRACER,
                            FtTimer.TIMER_ID_COMMAND_WAIT_PERIOD_CHECK, from,
                            FtTimer.DEFAULT_SHORT_DELAY);
                    retryCount++;
                }
                break;
            }
            case CommandInfo.COMMAND_STOP_MONITOR_GPS_POSITION: {
                Log.i(TAG, "COMMAND_STOP_MONITOR_GPS_POSITION");
                // mPm.removeSlavePhone(from);
                remove = true;
                mGps.stop();
                // to be attention
                FtTimer.stopTimer(EngineService.MODULE_GPS_TRACER, from);
                break;
            }

            case CommandInfo.COMMAND_VERIFY_SUCCESS:
                Log.i(TAG, "COMMAND_VERIFY_SUCCESS");
                Setting mSetting = SettingCreator.settingInit(mContext);
                String imsi = ((TelephonyManager) mContext
                        .getSystemService(Context.TELEPHONY_SERVICE))
                        .getSubscriberId();
                mSetting.setOldImsi(imsi);
                break;
            default:
                break;
        }
        return remove;
    }

    public void associatedPhoneGpsPositionChanged(PositionInfo position) {
        Log.i(TAG, "Show map changed : " + position.toString());
        if (getPhoneStatus() == PHONE_STATUS_WAIT_GPS_INFO) {
            mMap.showGpsPositionAtMap(getPhoneNum(), position);
        }
    }

    public void localGpsPositionChanged(PositionInfo position) {
        if (getPhoneStatus() == PHONE_STATUS_SEND_GPS_INFO) {
            mChannelProxy.send(getPhoneNum(), position);
        }
    }

    private void showMap(String from) {
        Intent intent = new Intent(mContext, ToShowMapActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(ToShowMapActivity.LOST_PHONE_NUM, from);
        mContext.startActivity(intent);
    }

    // public void startTrace() {
    // Log.e(TAG, "uiStartTrace " + getPhoneNum());
    // setPhoneStatus(PHONE_STATUS_WIAT_HANDSHANKE_CNF);
    // mChannelProxy.send(getPhoneNum(), new CommandInfo(
    // CommandInfo.COMMAND_HANDSHAKE_REQ),ChannelProxy.CHANNEL_SMS);
    // }

    public void startTraceShake() {
        setPhoneStatus(PHONE_STATUS_WIAT_HANDSHANKE_CNF);

        mChannelProxy.send(getPhoneNum(), new CommandInfo(
                CommandInfo.COMMAND_HANDSHAKE_REQ), ChannelProxy.CHANNEL_SMS);
        FtTimer.startTimer(EngineService.MODULE_GPS_TRACER,
                FtTimer.TIMER_ID_COMMAND_WAIT_HANDSHANKE_CNF, getPhoneNum());
        retryCount++;
    }

    public void stopTrace() {
        Log.e(TAG, "uiStopTrace " + getPhoneNum());
        // if (getPhoneStatus() == PHONE_STATUS_WAIT_GPS_INFO) {
        mChannelProxy.send(getPhoneNum(), new CommandInfo(
                CommandInfo.COMMAND_STOP_MONITOR_GPS_POSITION),
                ChannelProxy.CHANNEL_SMS);
        FtTimer.stopTimer(EngineService.MODULE_GPS_TRACER, getPhoneNum());
        mChannelProxy.stopHttpRecvPositionReport();
        // }
    }

    public void verifySuccess() {
        Log.i(TAG, "COMMAND_SEND_VERIFY_SUCCESS");
        mChannelProxy.send(getPhoneNum(), new CommandInfo(
                CommandInfo.COMMAND_VERIFY_SUCCESS), ChannelProxy.CHANNEL_SMS);
    }
}
